﻿Imports System.Net.Sockets
Imports System.Net
Imports System.Threading
Imports System.Text
Imports System.IO
Public Class BNetworking
    Public Shared T As New TcpClient()
    Public Shared NS As NetworkStream
    Public Shared MessageQueue As New Queue
    Public Shared WriteMessageThread As Thread

    Public Shared Sub Connect()
        Try
            If T.Connected = False Then
                Try
                    T = New System.Net.Sockets.TcpClient
                    T.Connect(Settings.Server, 6112)
                Catch ex As Exception
                    Console.WriteLine("CONNECT FUNCTION ERROR: " + ErrorToString())
                End Try
                If T.Connected = True Then
                    NS = New System.Net.Sockets.NetworkStream(T.Client)
                    NS = T.GetStream
                    TextHandling.QueueMessage("", "Connected", "System")
                    NS.WriteByte(&H3)
                    NS.WriteByte(&H4)
                    'TextFormatting.SystemMessage("Connected.")
                    Login()
                Else
                    TextHandling.QueueMessage("", "Error in Connecting. Server may be offline.", "System")
                    'TextFormatting.ErrorMessage("Error in Connecting.")
                End If
            Else
                TextHandling.QueueMessage("", "Connection request already sent. Please logout before trying to login again.", "Error")
            End If
        Catch ex As Exception
            Console.WriteLine("CONNECT FUNCTION ERROR: " + ErrorToString())
        End Try
    End Sub

    Public Shared Sub Login()
        Try
            Dim UserName As Byte() = Encoding.ASCII.GetBytes(Settings.UserName)
            Dim PassWord As Byte() = Encoding.ASCII.GetBytes(Settings.Password)
            NS.WriteByte(&HD)
            NS.Write(UserName, 0, UserName.Length())
            NS.WriteByte(&HD)
            NS.Write(PassWord, 0, PassWord.Length())
            NS.WriteByte(&HD)
        Catch ex As Exception
            Console.WriteLine("LOGIN FUNCTION ERROR: " + ErrorToString())
        End Try
    End Sub

    Public Shared Sub ConnectionClosed()
        Try
            BNetworking.NS.Flush()
            BNetworking.T.Client.Close()
            MainForm.Text = Settings.MainFormText
            Settings.CurrentChannel = "NOCHANNEL"
            MainForm.UserBox.Items.Clear()
            TextHandling.UpdateChannelLabel()
            WriteMessageThread.Abort()
            MainForm.t.Abort()
            MainForm.t2.Abort()
        Catch ex As Exception
            Console.WriteLine("CONNECTION CLOSED FUNCTION ERROR: " + ErrorToString())
        End Try
    End Sub

    Public Sub WriteMessage(ByVal Message As String)
        Try
            MessageQueue.Enqueue(Message)
        Catch ex As Exception
            Console.WriteLine("WRITE MESSAGE FUNCTION ERROR: " + ErrorToString())
        End Try
    End Sub

    Public Shared Sub WriteMessageT()
        Try
            Do
                If MessageQueue.Count <> 0 Then
                    If BNetworking.T.Connected Then
                        Dim MTBW As Byte() = Encoding.ASCII.GetBytes(MessageQueue.Peek)
                        BNetworking.NS.Write(MTBW, 0, MTBW.Length)
                        BNetworking.NS.WriteByte(&HD)
                        MessageQueue.Dequeue()
                    End If
                    Thread.Sleep(200)
                End If
                Thread.Sleep(50)
            Loop
        Catch ex As Exception
            Console.WriteLine("WRITE MESSAGE THREAD FUNCTION ERROR: " + ErrorToString())
        End Try
    End Sub
End Class